wifi.setmode(1)
wifi.sta.config("TP-LINK_S00","shuang00")
--print(wifi.sta.getip())

led1 = 0
--led2 = 4

doorled=1

gpio.mode(led1, gpio.OUTPUT)
gpio.write(led1,gpio.HIGH)
--gpio.mode(led2, gpio.OUTPUT)
--gpio.write(led2,gpio.LOW)

gpio.mode(doorled, gpio.OUTPUT)
gpio.write(doorled,gpio.HIGH)

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end
        buf = buf.."<h1> ESP8266 Web Server</h1>";
        buf = buf.."<p>DOORLED <a href=\"?pin=ON1\"><button>ON</button></a><a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        --buf = buf.."<p>GPIO2   <a href=\"?pin=ON2\"><button>ON</button></a><a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        print(_GET.pin)
        if(_GET.pin == "ON1")then
              gpio.write(led1, gpio.LOW);
              gpio.write(doorled, gpio.LOW);
        elseif(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.HIGH);
              gpio.write(doorled, gpio.HIGH);
       -- elseif(_GET.pin == "ON2")then
       --      gpio.write(led2, gpio.LOW);
       -- elseif(_GET.pin == "OFF2")then
       --       gpio.write(led2, gpio.HIGH);
        end

        client:send(buf);
        client:close();
        
        tmr.alarm(0, 60000, 1, function()
            gpio.write(led1, gpio.HIGH);
            gpio.write(doorled, gpio.HIGH);
        end )

        collectgarbage();
    end)
end)
